package com.unitd.frame.comm.utils.encrypt.base64;

import com.unitd.frame.comm.utils.encrypt.base64.encoder.IEncoder;
import com.unitd.frame.comm.utils.encrypt.base64.encoder.impl.UrlBase64Encoder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @desc URLBase64编解码工具类
 * URL安全的Base64编码说明：
 * Base64编码可用于在HTTP环境下传递较长的标识信息.在其他应用程序中,也常常需要把二进制数据编码为适合放在URL
 * (包括隐藏表单域)中的形式.此时,采用Base64编码不仅比较简短,同时也具有不可读性,即所编码的数据不会被人用肉眼所直接看到.
 * Convert binary data to and from UrlBase64 encoding. This is identical to
 * Base64 encoding, except that the padding character is "." and the other
 * non-alphanumeric characters are "-" and "_" instead of "+" and "/".
 * The purpose of UrlBase64 encoding is to provide a compact encoding of binary
 * data that is safe for use as an URL parameter. Base64 encoding does not
 * produce encoded values that are safe for use in URLs, since "/" can be
 * interpreted as a path delimiter; "+" is the encoded form of a space; and "="
 * is used to separate a name from the corresponding value in an URL parameter.
 */
public class UrlBase64 {
	private static final IEncoder encoder = new UrlBase64Encoder();

	/**
	 * @desc 对输入数据进行编码,生成一个base64编码的字节数组
	 * @param data 需要进行编码的字节数组
	 * @return 进行了base64编码的字节数组
	 */
	public static byte[] encode(byte[] data) {
		ByteArrayOutputStream bOut = new ByteArrayOutputStream();

		try {
			encoder.encode(data, 0, data.length, bOut);
		} catch (Exception e) {
			throw new IllegalStateException("exception encoding URL safe base64 data: " + e.getMessage(), e);
		}

		return bOut.toByteArray();
	}

	/**
	 * @desc 将字节数据编码为64,将其写入给定的输出流
	 * @param data 需要进行编码的字节数组
	 * @param out 输出流
	 * @return 生成的字节数
	 * @throws IOException
	 */
	public static int encode(byte[] data, OutputStream out) throws IOException {
		return encoder.encode(data, 0, data.length, out);
	}

	/**
	 * @desc 解码base64编码的输入数据, 假设输入数据是有效的
	 * @param data 需要进行解码的字节数组
	 * @return 解码后字节数组
	 */
	public static byte[] decode(byte[] data) {
		ByteArrayOutputStream bOut = new ByteArrayOutputStream();

		try {
			encoder.decode(data, 0, data.length, bOut);
		} catch (Exception e) {
			throw new IllegalStateException("exception decoding URL safe base64 string: " + e.getMessage(), e);
		}

		return bOut.toByteArray();
	}

	/**
	 * @desc 解码base64编码的字符串数据——空格将被忽略
	 * @param data 需要进行解码的字符串
	 * @param out 输出流
	 * @return 解码后字节数组
	 */
	public static int decode(byte[] data, OutputStream out) throws IOException {
		return encoder.decode(data, 0, data.length, out);
	}

	/**
	 * @desc 解码base64编码的字符串数据——空格将被忽略
	 * @param data 需要进行解码的字符串
	 * @return 解码后字节数组
	 */
	public static byte[] decode(String data) {
		ByteArrayOutputStream bOut = new ByteArrayOutputStream();

		try {
			encoder.decode(data, bOut);
		} catch (Exception e) {
			throw new IllegalStateException("exception decoding URL safe base64 string: " + e.getMessage(), e);
		}

		return bOut.toByteArray();
	}

	/**
	 * @desc  解码base64编码的字符串数据,将其写入给定的输出流,空格字符将被忽略
	 * @param data base64编码的字符串
	 * @param out 输出流
	 * @return 生成的字节数
	 * @throws IOException
	 */
	public static int decode(String data, OutputStream out) throws IOException {
		return encoder.decode(data, out);
	}
}